package com.novabluetooth.views;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.health.R;
import com.health.db.FDbManager;
import com.health.tablebean.SBCL_DATA;
import com.health.viewscontrols.NovaApplication;
import com.novabluetooth.adapter.ExamHistoryAdaptor;
import com.novabluetooth.adapter.ExamHistoryWeightAdaptor;
import com.novabluetooth.services.BluetoothConstant;

import java.util.List;

/**
 * 体检结果标注
 * 
 * @author nova
 *
 */
public class ExamHistoryWeightActivity extends Activity implements OnItemClickListener {
	public static final int ResultXueya = 1;
	public static final int ResultTizhong = 2;
	public static final int ResultXuetang = 3;
	public static final int ResultXueyang = 4;
	public static final int ResultTiwen = 5;
	public static final int ResultXueHongDanbai = 6;
	public static final int ResultNiaoChangGui = 7;
	public static final int ResultMaiLv = 8;// 心率等同于脉率
	public static final int ResultECG = 9;
	public static final int ResultXueZhi = 10;
	/**
	 * 百捷三合一
	 */
	public static final int ResultTRIPLE = 10;
	/**
	 * 胆固醇
	 */
	public static final int ResultDGC = 11;
	private static final String TAG = "ExamHistoryActivity";
	private ListView lv;
	private Button cancelBtn;
	static FDbManager fdbmanger = NovaApplication.fdbmanger;
	private ExamHistoryWeightAdaptor adaptor;
	private String flag;
	private List<SBCL_DATA> gaoList;
	private List<SBCL_DATA> diya;
	private List<SBCL_DATA> mailv;
	private List<SBCL_DATA> dataList;
	List<SBCL_DATA> niaodanyuanList;// 尿胆原
	List<SBCL_DATA> qianxueList;// 潜血
	List<SBCL_DATA> danhongsuList;// 胆红素
	List<SBCL_DATA> tongtiList;// 酮体
	List<SBCL_DATA> putaotangList;// 葡萄糖
	List<SBCL_DATA> danbaizhiList;// 蛋白质
	List<SBCL_DATA> pHList;// ph
	List<SBCL_DATA> yaxiaoList;// 亚硝酸盐
	List<SBCL_DATA> baixibaoList;// 白细胞
	List<SBCL_DATA> bizhongList;// 比重
	List<SBCL_DATA> weishengsuList;// 维生素

	private static String IDCARD = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.exam_history_weight_list);
		flag = getIntent().getStringExtra("flag");
		lv = (ListView) findViewById(R.id.exam_history_lv);
		cancelBtn = (Button) findViewById(R.id.exam_history_cancel);

		if (NovaApplication.getUserInfo() != null) {
			IDCARD = NovaApplication.getUserInfo().getIDCARD();
			if (TextUtils.isEmpty(IDCARD)) {
				return;
			}
		}

		setSize(0.9, 0.7);
		// 体重 BMI 肌肉量  脂肪率  骨量  皮下脂肪  骨骼肌率  水分率  肌肉率  蛋白质 健康评估 肥胖等级 健康等级 测量时间

		dataList = fdbmanger.findAllByWhere(SBCL_DATA.class,
				"DEVICE_TYPE = '轻加脂肪秤' AND IDCARD = '" + IDCARD + "'", "CL_TIME desc");
		String[] tvtext = { "体重", "BMI", "肌肉量", "脂肪率", "骨量", "皮下脂肪", "水分率",
				"蛋白质", "健康评估","肥胖等级","健康等级",
				"身体年龄", "理想体重","身体类型","身体得分","肌肉率","脂肪量","去脂体重","体重控制","脂肪控制量",
				"骨骼肌率","肌肉控制量","内脏脂肪","基础代谢",
				"检测时间" };
		adaptor = new ExamHistoryWeightAdaptor(dataList, this, flag);
		setHead(tvtext);

		lv.setAdapter(adaptor);
		lv.setOnItemClickListener(this);

		cancelBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

	}

	private void setSize(double width, double height) {
		WindowManager m = getWindowManager();
		Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
		LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
		p.height = (int) (d.getHeight() * height); // 高度设置为屏幕的1.0
		p.alpha = 0.9f; // 设置本身透明度
		p.dimAmount = 0.0f; // 设置黑暗度
		p.width = (int) (d.getWidth() * width); // 宽度设置为屏幕的0.8
		getWindow().setAttributes(p);
	}

	private void setHead(String[] tvTexts) {
		TextView tv1 = (TextView) findViewById(R.id.exam_history_tv1);
		TextView tv2 = (TextView) findViewById(R.id.exam_history_tv2);
		TextView tv3 = (TextView) findViewById(R.id.exam_history_tv3);
		TextView tv4 = (TextView) findViewById(R.id.exam_history_tv4);
		TextView tv5 = (TextView) findViewById(R.id.exam_history_tv5);
		TextView tv6 = (TextView) findViewById(R.id.exam_history_tv6);
		TextView tv7 = (TextView) findViewById(R.id.exam_history_tv7);
		TextView tv8 = (TextView) findViewById(R.id.exam_history_tv8);
		TextView tv9 = (TextView) findViewById(R.id.exam_history_tv9);
		TextView tv10 = (TextView) findViewById(R.id.exam_history_tv10);
		TextView tv11 = (TextView) findViewById(R.id.exam_history_tv11);
		TextView tv12 = (TextView) findViewById(R.id.exam_history_tv12);
		TextView tv13 = (TextView) findViewById(R.id.exam_history_tv13);
		TextView tv14 = (TextView) findViewById(R.id.exam_history_tv14);
		TextView tv15 = (TextView) findViewById(R.id.exam_history_tv15);
		TextView tv16 = (TextView) findViewById(R.id.exam_history_tv16);
		TextView tv17 = (TextView) findViewById(R.id.exam_history_tv17);
		TextView tv18 = (TextView) findViewById(R.id.exam_history_tv18);
		TextView tv19 = (TextView) findViewById(R.id.exam_history_tv19);
		TextView tv20 = (TextView) findViewById(R.id.exam_history_tv20);
		TextView tv21 = (TextView) findViewById(R.id.exam_history_tv21);
		TextView tv22 = (TextView) findViewById(R.id.exam_history_tv22);
		TextView tv23 = (TextView) findViewById(R.id.exam_history_tv23);
		TextView tv24 = (TextView) findViewById(R.id.exam_history_tv24);
		TextView timeTv = (TextView) findViewById(R.id.exam_history_time);
		TextView[] tvs = { tv1, tv2, tv3, tv4, tv5, tv6, tv7, tv8, tv9, tv10, tv11,tv12,tv13,tv14,
				tv15,tv16,tv17,tv18,tv19,tv20,tv21,tv22,tv23,tv24,timeTv };

		for (TextView textView : tvs) {
			if (textView.getVisibility() == View.VISIBLE) {
				textView.setVisibility(View.GONE);
			}
		}
		int length = tvTexts.length;
		for (int i = 0; i < length - 1; i++) {
			tvs[i].setVisibility(View.VISIBLE);
			tvs[i].setText(tvTexts[i]);
		}
		tvs[tvs.length - 1].setVisibility(View.VISIBLE);
		tvs[tvs.length - 1].setText(tvTexts[length - 1]);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent();
		if (BluetoothConstant.XUEYA_TYPENAME.equals(flag)) {
			intent.putExtra("高压", gaoList.get(position).getCL_VALUE().split("mmHg")[0]);
			intent.putExtra("低压", diya.get(position).getCL_VALUE().split("mmHg")[0]);
			intent.putExtra("脉率", mailv.get(position).getCL_VALUE().split("次")[0]);
			Log.e(TAG, gaoList.get(position).getCL_VALUE().split("mmHg")[0]);
			setResult(ResultXueya, intent);

		} else if (BluetoothConstant.TIZHONG_TYPENAME.equals(flag)) {
			intent.putExtra("value", dataList.get(position).getCL_VALUE().split("Kg")[0]);
			setResult(ResultTizhong, intent);
		} else if (BluetoothConstant.XUETANG_TYPENAME.equals(flag)) {
			intent.putExtra("value", dataList.get(position).getCL_VALUE().split("mmol/L")[0]);
			setResult(ResultXuetang, intent);
		} else if (BluetoothConstant.XUEYANG_TYPENAME.equals(flag)) {
			intent.putExtra("value", dataList.get(position).getCL_VALUE().split("%")[0]);
			setResult(ResultXueyang, intent);
		} else if(BluetoothConstant.XUEZHI_TYPENAME.equals(flag)){
			intent.putExtra("value", dataList.get(position).getCL_VALUE());
			setResult(ResultXueZhi, intent);
		}else if (BluetoothConstant.TIWEN_TYPENAME.equals(flag)) {
			intent.putExtra("value", dataList.get(position).getCL_VALUE().split("℃")[0]);
			setResult(ResultTiwen, intent);
		} else if ("血红蛋白".equals(flag)) {
			intent.putExtra("value", dataList.get(position).getCL_VALUE());
			setResult(ResultXueHongDanbai, intent);
		} else if ("脉率".equals(flag) || "心率".equals(flag)) {
			String value_xl = dataList.get(position).getCL_VALUE();
			if (value_xl.contains("次/分")) {
				value_xl = value_xl.split("次/分")[0];
			} else if (value_xl.contains("bpm")) {
				value_xl = value_xl.split("bpm")[0];
			}
			intent.putExtra("value", value_xl);
			setResult(ResultMaiLv, intent);
		} else if (BluetoothConstant.ECG_TYPENAME.equals(flag)) {
			Log.e("eeeeeeeeeeeeeeeee", dataList.get(position).getCL_VALUE());
			intent.putExtra("result", dataList.get(position).getCL_VALUE());
			intent.putExtra("time", dataList.get(position).getCL_TIME());
			setResult(ResultECG, intent);
		} else if (BluetoothConstant.NIAOCHANGGUI_TYPENAME.equals(flag)) {
			intent.putExtra("尿胆原", niaodanyuanList.get(position).getCL_VALUE());
			intent.putExtra("尿潜血", qianxueList.get(position).getCL_VALUE());
			intent.putExtra("胆红素", danhongsuList.get(position).getCL_VALUE());
			intent.putExtra("尿酮体", tongtiList.get(position).getCL_VALUE());
			intent.putExtra("尿糖", putaotangList.get(position).getCL_VALUE());
			intent.putExtra("蛋白质", danbaizhiList.get(position).getCL_VALUE());
			intent.putExtra("PH", pHList.get(position).getCL_VALUE());
			intent.putExtra("亚硝酸盐", yaxiaoList.get(position).getCL_VALUE());
			intent.putExtra("白细胞", baixibaoList.get(position).getCL_VALUE());
			intent.putExtra("比重", bizhongList.get(position).getCL_VALUE());
			intent.putExtra("维生素", weishengsuList.get(position).getCL_VALUE());
			setResult(ResultNiaoChangGui, intent);
		}else if (BluetoothConstant.DGC_TYPENAME.equals(flag)) {
			intent.putExtra("value", dataList.get(position).getCL_VALUE().split("Kg")[0]);
			setResult(ResultDGC, intent);
		}
		finish();
	}


}
